package com.ybkj.daijia.server.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.config.PartnerApi;
import com.ybkj.daijia.server.partner.Intent;
import com.ybkj.daijia.server.service.IntentService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DefaultIntentService implements IntentService {

    @Autowired
    private PartnerApi partnerApi;
    @Autowired
    private SettingUtils settingUtils;

    @Override
    public Intent save(Intent partner) {
        StringBuilder sb = new StringBuilder();
        sb.append(partnerApi.getPartnerBaseURL() + PartnerApi.SAVE_INTENT
            + ".json?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("categoryAlias", partner.getCategoryAlias());
        sb.append("&categoryAlias={categoryAlias}");

        kv.put("name", partner.getName());
        sb.append("&name={name}");

        kv.put("alias", partner.getAlias());
        sb.append("&alias={alias}");

        String result = partnerApi.post(sb.toString(), kv, settingUtils.get());

        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new Gson();
                Intent intent = gson.fromJson(data.toString(), new TypeToken<Intent>() {
                }.getType());
                return intent;
            }
        }
        return null;
    }

    @Override
    public Intent findById(Long id) {
        Intent intent = new Intent();
        StringBuilder sb = new StringBuilder();
        sb.append(partnerApi.getPartnerBaseURL() + PartnerApi.QUERY_ONE_INTENT
            + "?appKey={appKey}&appId={appId}&sign={sign}");
        Map<String, String> kv = new HashMap<String, String>();
        kv.put("id", String.valueOf(id));
        sb.append("&id={id}");
        String string = partnerApi.get(sb.toString(), kv, settingUtils.get());
        JSONObject jo = new JSONObject(string);
        Integer code = jo.getInt("code");
        if (code == 0) {
            JSONObject data = jo.getJSONObject("data");
            Gson gson = new Gson();
            intent = gson.fromJson(data.toString(), new TypeToken<Intent>() {
            }.getType());
            return intent;
        } else {
            return null;
        }
    }

    @Override
    public List<Intent> findByCategoryAlias(String categoryAlias) {
        List<Intent> list = new ArrayList<Intent>();

        StringBuilder sb = new StringBuilder();
        sb.append(partnerApi.getPartnerBaseURL() + PartnerApi.QUERY_INTENT
            + ".json?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("categoryAlias", categoryAlias);
        sb.append("&categoryAlias={categoryAlias}");

        String result = partnerApi.get(sb.toString(), kv, settingUtils.get());

        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONArray data = jo.getJSONArray("data");
                Gson gson = new Gson();
                list = gson.fromJson(data.toString(), new TypeToken<List<Intent>>() {
                }.getType());
            }
        }
        return list;
    }

    @Override
    public Intent updateIntent(Intent intent) {
        StringBuilder builder = new StringBuilder();
        builder.append(partnerApi.getPartnerBaseURL() + PartnerApi.UPDATE_INTENT + intent.getId()
            + "?appKey={appKey}&appId={appId}&sign={sign}");
        Map<String, String> kv = new HashMap<String, String>();
        kv.put("name", intent.getName());
        builder.append("&name={name}");
        String result = "false";
        result = partnerApi.put(builder.toString(), kv, settingUtils.get());
        if (StringUtils.isBlank(result)) {
            return intent;
        } else {
            return null;
        }
    }

}